#include "Task.hpp"
#include "ThreadPool.hpp"
#include "Log.hpp"

using namespace log_ns;

// int main()
// {
//     srand(time(nullptr)^getpid());
//     int cnt = 10;
//     while (cnt--)
//     {
//         int a = rand() % 100 + 1, b = rand() % 100 + 1;
//         Task t(a, b);
//         ThreadPool<Task>::GetInstance()->Equeue(t);
//         LOG(INFO, "Equeue Task - %d\n", 10 - cnt);
//     }
//     ThreadPool<Task>::GetInstance()->Stop();
//     sleep(2);
//     return 0;
// }



void Test(int _NUM_, int cnt)
{
    while (cnt--)
    {
        int a = rand() % 100 + 1, b = rand() % 100 + 1;
        Task t(a, b);
        ThreadPool<Task>::GetInstance()->Equeue(t);
        usleep(100);
    }
    // ThreadPool<Task>::GetInstance()->Stop();
    usleep(1000);
    if (__CNT__ == 0)// if 判断__CNT__是否为预期值
        __CNT__ = 10; // 是预期值就重置
    else
    {
        char buffer[1024] = {0};
        snprintf(buffer, sizeof(buffer), "This is Error: __CNT__ = %d, _NUM_ = %d, cnt = %d\n", __CNT__, _NUM_, cnt);
        throw buffer;
        // 不是预期值就返回退出
    }
}
int main()
{
    srand(time(nullptr) ^ getpid());
    int _NUM_ = 100; // 测试次数
    while (_NUM_--)
    {
        int cnt = 10;
        try
        {
            Test(_NUM_, cnt);
        }
        catch (const char* s)
        {
            printf("%s\n", s);
        }

        
    }
    std::cout<<"OK"<<std::endl;
    return 0;
}